import requests
import json
import time

class BitfinexInfo(object):
  def __init__(self):
    self.urls = {
      'ticker': 'https://api.bitfinex.com/v1/pubticker/%s',
      'symbol': 'https://api.bitfinex.com/v1/symbols_details',
    }
    self.archive_file_name = 'bitfinex.json'
    self.info = {}

  def get_symbol_info(self, symbol):
    url = self.urls['ticker'] % symbol 
    return requests.get(url).json()

  def get_symbol_list(self):
    url = self.urls['symbol']
    symlist = requests.get(url).json()
    return [info['pair'] for info in symlist]

  def get_info(self):
    for symbol in self.get_symbol_list():
      symbol_info = self.get_symbol_info(symbol)
      self.info[symbol] = symbol_info
      print(json.dumps(symbol_info, indent=2))
      time.sleep(6)

  def save(self):
    with open(self.archive_file_name, 'w') as outfile:
      json.dump(self.info, outfile, indent=2)
  

def main():
  bitfinex_info = BitfinexInfo()
  bitfinex_info.get_info();
  bitfinex_info.save()


if __name__ == '__main__':
  main()
